cmake_minimum_required (VERSION 3.7.2) 

if (POLICY CMP0074)
    cmake_policy (SET CMP0074 NEW)
endif ()

if (USE_CLANG)
    SET (ENV{CC} clang)
    SET (ENV{CXX} clang++)
endif ()

list (INSERT CMAKE_MODULE_PATH 0 "${CMAKE_SOURCE_DIR}/cmake")

project (SRPGBuilder)

# Specify default build type if none provided (before project() command)
if (NOT CMAKE_BUILD_TYPE)
    set (CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build (Debug or Release)" FORCE)
endif ()

# For generators with multiple configurations (like VS), only allow Debug and Release
if (CMAKE_CONFIGURATION_TYPES)
    set (CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Limited configurations" FORCE)
    mark_as_advanced (CMAKE_CONFIGURATION_TYPES)
endif ()

set (CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_STANDARD_REQUIRED ON)

include (AddPrecompiledHeader)

set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Predefined configuration options
set (SRPGB_SHARED_LIBS TRUE CACHE BOOL "Build shared libraries (use shared SFML librares)")

# Windows: Choose to link CRT libraries statically or dynamically
if (WIN32)
    set (SRPGB_STATIC_STD_LIBS FALSE CACHE BOOL "Use statically linked standard/runtime libraries? This option must match the one used for SFML.")

    # Determine whether we're dealing with a TDM compiler or not
    if (CMAKE_COMPILER_IS_GNUCXX)
        execute_process (COMMAND "${CMAKE_CXX_COMPILER}" "--version" OUTPUT_VARIABLE GCC_COMPILER_VERSION)
        string (REGEX MATCHALL ".*(tdm[64]*-[1-9]).*" COMPILER_GCC_TDM "${GCC_COMPILER_VERSION}")
    endif()

    # Modify compiler flags globally
    if (SRPGB_STATIC_STD_LIBS)
        if (SRPGB_SHARED_LIBS)
            message ("\n-> SRPGB_STATIC_STD_LIBS and SRPGB_SHARED_LIBS are not compatible.")
            message ("-> They lead to multiple runtime environments which result in undefined behavior.\n")
        elseif (MSVC)
            foreach (flag CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE)
                if (${flag} MATCHES "/MD")
                    string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}")
                endif ()
            endforeach ()
        elseif (CMAKE_COMPILER_IS_GNUCXX AND NOT COMPILER_GCC_TDM)
            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
        endif ()
    elseif (COMPILER_GCC_TDM)
        set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared-libgcc -shared-libstdc++")
    endif ()
endif ()

# Compiler-specific flags and definitions
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    set (SRPGB_CLANG_STDLIB "" CACHE STRING "Chose standard library (libstdc++ or libc++) or leave empty for default")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wno-switch -Wno-logical-op-parentheses")
    if (NOT SRPGB_CLANG_STDLIB STREQUAL "")
        set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=${SRPGB_CLANG_STDLIB}")
    endif ()
elseif (CMAKE_COMPILER_IS_GNUCXX)
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14")
endif ()

# Find SFML&TGUI
if (NOT SRPGB_SHARED_LIBS)
    set (SFML_STATIC_LIBRARIES TRUE)
    set (TGUI_STATIC_LIBRARIES TRUE)
endif ()

if (CMAKE_VERSION VERSION_LESS 3.12)
    if (SFML_ROOT)
        set (SFML_DIR "${SFML_ROOT}/lib/cmake/SFML")
    endif ()
    if (TGUI_ROOT)
        set (TGUI_DIR "${TGUI_ROOT}/lib/cmake/TGUI")
    endif ()
endif ()

find_package (SFML 2 COMPONENTS audio graphics window system)

if (NOT SFML_FOUND)
    set (SFML_ROOT "" CACHE PATH "SFML top-level directory")
    message ("\n-> SFML directory not found. Set SFML_ROOT to SFML's top-level path (containing \"include\" and \"lib\" directories).")
    message ("-> Make sure the SFML libraries with the same configuration (Release/Debug, Static/Dynamic) exist.\n")
endif ()

find_package (TGUI)
if (NOT TGUI_FOUND)
    set (TGUI_ROOT "" CACHE PATH "TGUI top-level directory")
    message ("\n-> TGUI directory not found. Set TGUI_ROOT to TGUI's top-level path (containing \"include\" and \"lib\" directories).")
    message ("-> Make sure the TGUI libraries with the same configuration (Release/Debug, Static/Dynamic) exist.\n")
endif ()

# Enable IDE folders and set them for predefined CMake projects
set_property (GLOBAL PROPERTY USE_FOLDERS ON)
set_property (GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMake")

add_subdirectory (deps)
include_directories (deps/yaml-cpp/include)

add_subdirectory (common)
add_subdirectory (loader)
add_subdirectory (editor)
